use pest::iterators::Pair;
use pest::Parser;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "lexical/lexer.pest"]
pub struct LexicalParser;

#[allow(non_camel_case_types)]
#[derive(Debug, PartialEq)]
pub struct Token<'a> {
    pub pair: Pair<'a, Rule>,
}

impl<'a> From<Pair<'a, Rule>> for Token<'a> {
    fn from(pair: Pair<'a, Rule>) -> Self {
        Token { pair }
    }
}

pub fn tokenize(input: &str) -> Option<Vec<Token>> {
    Some(
        LexicalParser::parse(Rule::SysY, input)
            .unwrap()
            .next()?
            .into_inner()
            .map(Token::from)
            .collect(),
    )
}
